package com.allenkerr.solr;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by 小柯(Allen Kerr) on 17.5.1 20:58.
 */
public class IndexSearch {

    public static void searchIndex1() throws SolrServerException, IOException {
        //1. 创建与solr服务器的连接
        SolrServer solrServer = SolrJUtils.getSolrServer();
        //2. 创建solr查询条件对象
        SolrQuery solrQuery = new SolrQuery();
        //3. 查询所有
        solrQuery.setQuery("*:*");
        //4. 查询并获取查询响应对象
        QueryResponse response = solrServer.query(solrQuery);
        //5. 从查询响应对象中获取查询结果集对象
        SolrDocumentList results = response.getResults();
        //6. 记录数
        System.out.println("总共有 " + results.getNumFound() + " 条记录。");
        //7. 遍历结果集
        for (SolrDocument result : results) {
            System.out.println("id: " + result.get("id"));
            System.out.println("product name: " + result.get("product_name"));
            System.out.println("product price: " + result.get("product_price") + "\n");
        }
    }

    public static void searchIndex2() throws SolrServerException, IOException {
        //1. 创建与solr服务器的连接
        SolrServer solrServer = SolrJUtils.getSolrServer();
        //2. 创建solr查询条件对象
        SolrQuery solrQuery = new SolrQuery();

        //3. 完善查询条件
        solrQuery.setQuery("礼品");                              //关键词
        solrQuery.set("df", "product_keywords");                 //默认搜索域
        solrQuery.addFilterQuery("product_price:[20 TO 50]");   //过滤条件
        solrQuery.setSort("product_price", SolrQuery.ORDER.desc);//排序方式
        solrQuery.setStart(0);                                  //分页设置
        solrQuery.setRows(30);                                  //分页设置
        ///////////////////////////高亮设置////////////////////////////////
        solrQuery.setHighlight(true);
        solrQuery.addHighlightField("product_name");                //需要高亮显示的域
        solrQuery.setHighlightSimplePre("<span style='color:red'>");//前缀
        solrQuery.setHighlightSimplePost("</span>");                //后缀
        ///////////////////////////高亮设置////////////////////////////////

        //4. 查询并获取查询响应对象
        QueryResponse response = solrServer.query(solrQuery);
        //5. 从查询响应对象中获取查询结果集对象
        SolrDocumentList results = response.getResults();
        //6. 记录数
        System.out.println("总共有 " + results.getNumFound() + " 条记录。");
        //7. 遍历结果集
        int i = 1;
        for (SolrDocument result : results) {
            System.out.println("----------" + i++ + "----------");
            System.out.println("id: " + result.get("id"));
            //获取高亮
            Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
            List<String> list = highlighting.get(result.get("id")).get("product_name");
            if (list != null && list.size() > 0) {
                String highlightingName = list.get(0);
                System.out.println("高亮 product name: " + highlightingName);
            } else {
                System.out.println("普通 product name: " + result.get("product_name"));
            }
            System.out.println("product price: " + result.get("product_price") + "\n");
        }
    }

}
